﻿/*
版权所有：版权所有(C) 2010，刘建新
文件编号：M01_ProductsTypeBll.cs
文件名称：ProductsTypeBll.cs
系统编号：Z0003007
系统名称：通用办公系统
模块编号：M01
模块名称：信息发布
设计文档：IOA_M01信息发布XDE建模.mdx
完成日期：2010/12/17
作　　者：
内容摘要：表[Products_Type]业务操作类
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Eis51.CampanyCMS.Model.Bus;
using System.Collections;
using Eis51.CampanyCMS.Model;
using System.Web.UI.WebControls;
using Eis51.CampanyCMS.Business.BaseInfoMng;
using System.Data;
using Eis51.CampanyCMS.FrameWork;
using Eis51.CampanyCMS.Access.Bus;

namespace Eis51.CampanyCMS.Business.Bus
{
    /// <summary>
	/// ProductsType的BLL 自定义方法
	/// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
	/// </summary>
    public partial class ProductsTypeBLL : BaseBLL<ProductsType>
    {

        public void DeleteTreeProductType(int seqno)
        {
            string sql = "delete from  Products_Type where Seqno in( select Mychildid from MyAllChild where Mainid={0} and type='{1}') or  Seqno={0} ";
            sql = string.Format(sql, seqno,ParentType.ProductType.ToString("d"));
            this.Execute(sql);
        }


        /// <summary>
        /// 绑定
        /// </summary>
        public void BindProductType(ListControl listControl, string parentsId)
        {
            listControl.Items.Clear();
            // Products_Type([Type_CN_Name],[Type_EN_Name],[CN_Descr],[EN_Descr],[Parentid],[Level],[Create_Date],[Create_By],[Last_update_Date],[Last_update_By]
            //string sql = "select * from Products_Type where Parentid = {0}";
            //sql = string.Format(sql, parentsId);
            //DataTable dt = ProductsTypeBLL.GetInstance().GetDataTable(sql);
            IList it = GetProductType(parentsId); //ProductsType.Dt2Objs(dt, ProductsType.ColumnNameEnum.DBName);

            if (AppContext.IsEnglish)
            {
                listControl.DataSource = it;
                listControl.DataTextField = "title";
                listControl.DataValueField = "seqno";
                listControl.DataBind();
                listControl.Items.Insert(0, new ListItem("Plase Choose", ""));
            }
            else
            {
                listControl.DataSource = it;
                listControl.DataTextField = "title";
                listControl.DataValueField = "seqno";
                listControl.DataBind();
                listControl.Items.Insert(0, new ListItem("请选择", ""));
            }
        }

        /// <summary>
        /// 提交保存信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SubMit(ProductsType obj)
        {
            if (obj.Seqno != 0)
            {
                obj.Lastupdateby = AppContext.CurrentAdministrator.UserName;
                obj.Lastupdatedate = DateTime.Now;
                ProductsType old = this.Get(new ProductsType.Key(obj.Seqno));
                this.Update(obj);
                if (obj.Parentid != old.Parentid)
                {
                    MyallchildBLL.GetInstance().ChangeParent(obj.Seqno, obj.Parentid, ParentType.ProductType.ToString("d"));
                }
            }
            else
            {
                obj.Createdate = DateTime.Now;
                obj.Createby = AppContext.CurrentAdministrator.UserName;
                obj.Lastupdateby = AppContext.CurrentAdministrator.UserName;
                obj.Lastupdatedate = DateTime.Now;
                this.Insert(obj);
                //加父子关系
                if (obj.Parentid > 0)
                {
                    MyallchildBLL.GetInstance().AddParent(obj.Seqno, obj.Parentid, ParentType.ProductType.ToString("d"));
                }
                if (string.IsNullOrEmpty(obj.Code))
                {
                    obj.Code = obj.Seqno.ToString();
                    this.Update(obj);
                }
            }
            Dict.TypeTree.ClearCache();
            BuildPageBLL.CreateHtml(RunType.ProductType, obj);
            return true;
        }


        public static IList GetTypeList(string CreateBy, int Licd)
        {
            string sql = "select * from Products_Type where  Licd={0} order by  seqnoIndex desc ";
            sql = string.Format(sql, Licd);
            DataTable dt = ProductsTypeBLL.GetInstance().GetDataTable(sql);
            if (dt == null) return null;
            return ProductsType.Dt2Objs(dt, ProductsType.ColumnNameEnum.DBName);
        }



        /// <summary>
        /// 取得类型名
        /// </summary>
        /// <param name="productsTypeTreeId"></param>
        /// <returns></returns>
        public string GetPathName(string productsTypeTreeId)
        {
            string name = "";
            name = GetClassNameById(productsTypeTreeId, "～");
            return name;
        }

        /// <summary>
        /// 查询自己和自己的子节点
        /// </summary>
        /// <param name="Parentid"></param>
        /// <returns></returns>
        public string GetSubTreeType(string Parentid)
        {
            string temp = Parentid;
            string Returnvalue = temp;
            while (temp != string.Empty)
            {
                temp = GetSubTreeId(temp);
                if (temp != string.Empty)
                {
                    Returnvalue += "," + temp;
                }
            }
            return Returnvalue;
        }

        public string GetSubTreeId(string Parent_idList)
        {
            string tval = "";
            string sql = "select  seqno from Products_Type where Parentid  in( {0} ) ";
            sql = string.Format(sql, Parent_idList);
            DataTable dt = ProductsTypeBLL.GetInstance().GetDataTable(sql);
            foreach (DataRow dr in dt.Rows)
            {
                tval += dr["seqno"].ToString() + ",";
            }
            tval = tval.Trim(',');
            return tval;
        }


        /// <summary>
        /// 绑定
        /// </summary>
        public void BindProductTypeCheck(CheckBoxList chklist, string parentsId)
        {
            chklist.Items.Clear();
            IList it = GetProductType(parentsId);
            chklist.DataSource = it;
            chklist.DataTextField = "title";
            chklist.DataValueField = "seqno";
            chklist.DataBind();
        }

        public IList GetProductType(string typeid)
        {

            IList it = Dict.TypeTree.ProductsTypeTreeData;
            ArrayList ar = new ArrayList();
            foreach (ProductsType o in it)
            {
                if (typeid != "" && o.Parentid == int.Parse(typeid) && o.Level <= 1)
                {
                    if (o.Level == 1)
                    {
                        ProductsType OP = o.Clone() as ProductsType;
                        OP.Title = "-" + OP.Title;
                        ar.Add(OP);
                    }
                    else
                    {
                        ar.Add(o);
                    }
                    GetProductType(o.Seqno.ToString(), ar);
                }
            }
            return ar;
        }

        public void GetProductType(string typeid, ArrayList ar)
        {

            /*
            string sql = "select * from Products_Type where Parentid = {0}";
            sql = string.Format(sql, typeid);
            DataTable dt = this.GetDataTable(sql);
            IList it = ProductsType.Dt2Objs(dt, ProductsType.ColumnNameEnum.DBName);
            return it;
            */
            IList it = Dict.TypeTree.ProductsTypeTreeData;
            foreach (ProductsType o in it)
            {
                if (typeid != "" && o.Seqno == int.Parse(typeid) && o.Level == 2)
                {
                    ProductsType OP = o.Clone() as ProductsType;
                    OP.Title = "　　" + OP.Title;
                    ar.Add(OP);
                }
            }
        }



        private IList GetTreeIList(string ParentID)
        {

            string sql = "select * from Products_Type where Parentid =" + ParentID;
            DataTable dt = ProductsTypeBLL.GetInstance().GetDataTable(sql);
            IList it = ProductsType.Dt2Objs(dt, ProductsType.ColumnNameEnum.DBName);
            return it;
        }

        #region droplist


        /// <summary>
        /// 
        /// </summary>
        /// <param name="listControl"></param>
        public static void BindDropListTreeType(ListControl listControl, string CreateBy, int Licd)
        {
            string sql = string.Format("select * from Products_Type where CreateBy='{0}'  and Licd={1} ", CreateBy, Licd);
            DataTable dt = ProductsTypeBLL.GetInstance().GetDataTable(sql);
            DataTable dtNew = new DataTable();
            dtNew.Columns.Add("Parentid");
            dtNew.Columns.Add("title");
            dtNew.Columns.Add("seqno");
            ProductsTypeBLL ptree = new ProductsTypeBLL();
            ptree.TreeListData("0", dt, dtNew);

            ProductsTypeBLL bll = new ProductsTypeBLL();
            listControl.DataSource = dtNew;
            listControl.DataTextField = "title";
            listControl.DataValueField = "seqno";
            listControl.DataBind();
        }

        string symbolf = "";
        string symbol = "";
        public void TreeListData(string parentID, DataTable allTable, DataTable NewTable)
        {
            DataRow[] dataArr = allTable.Select("Parentid=" + parentID);
            int total = dataArr.Length;
            for (int i = 0; i < total; i++)
            {
                if ((i + 1) == total)
                    symbol = "└";
                else
                    symbol = "├";
                DataRow newRow = NewTable.NewRow();
                newRow["Parentid"] = dataArr[i]["Parentid"].ToString();
                newRow["title"] = symbolf + symbol + dataArr[i]["title"].ToString();
                newRow["seqno"] = dataArr[i]["seqno"].ToString();
                NewTable.Rows.Add(newRow);
                if ((i + 1) == total)
                    symbolf += "　";
                else
                    symbolf += "│";
                TreeListData(dataArr[i]["seqno"].ToString(), allTable, NewTable);
                symbolf = symbolf.Substring(0, symbolf.Length - 1);
            }
        }
        #endregion





        /// <summary>
        /// 获得产品类型描述
        /// </summary>
        /// <param name="productsTypeTreeId"></param>
        /// <returns></returns>
        public string ShowDescr(string productsTypeTreeId)
        {
            ProductsType typeTree = this.Get(new ProductsType.Key(int.Parse(productsTypeTreeId)));
            if (typeTree == null) return "";
            return typeTree.Showdescr;
        }

        /// <summary>
        /// 取得类型名
        /// </summary>
        /// <param name="productsTypeTreeId"></param>
        /// <returns></returns>

        public string GetClassNameByTreeId(string productsTypeTreeId)
        {
            ProductsType typeTree = Get(new ProductsType.Key(int.Parse(productsTypeTreeId)));
            if (typeTree == null) return "";
            return typeTree.Title;
        }


        /// <summary>
        /// 2010.7.21
        /// </summary>
        /// <param name="productsTypeTreeId"></param>
        /// <param name="ParentTreeId"></param>
        /// <returns></returns>
        public string GetParentNameById(int productsTypeTreeId, ref int ParentTreeId)
        {
            ProductsType typeTree =
                Get(new ProductsType.Key(productsTypeTreeId));
            if (typeTree != null)
            {
                ParentTreeId = typeTree.Parentid;
                return typeTree.Title;
            }
            return "";
        }

        /// <summary>
        /// 取得类型名
        /// </summary>
        /// <param name="productsTypeTreeId"></param>
        /// <returns></returns>

        public string GetClassNameById(string productsTypeTreeId, string code)
        {
            string name = "";
            ProductsType typeTree =
                Get(new ProductsType.Key(int.Parse(productsTypeTreeId)));
            if (typeTree == null) return "";
            int ParentTreeId = typeTree.Parentid;
            name = typeTree.Title;
            while (ParentTreeId > 0)
            {
                string className = GetParentNameById(ParentTreeId, ref ParentTreeId);
                if (className != string.Empty)
                {
                    name = className + code + name;
                }
            }
            return name;
        }

        /// <summary>
        /// 取得类型名
        /// </summary>
        /// <param name="productsId"></param>
        /// <returns></returns>
        public string GetClassNameByProductId(string productsId)
        {

            
            Products p = ProductsBLL.GetInstance().Get(new Products.Key(int.Parse(productsId)));
            if (p != null)
            {
                return GetClassNameById(p.Type.ToString(), "&gt;") + "&gt;" + p.Title;
            }
            return "";
        }

    }

    #region 代码自动生成
    /// <summary>
    /// ProductsType的BLL
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public partial class ProductsTypeBLL : BaseBLL<ProductsType>
    {
        #region 自动生成代码：取得相关实体

        /// <summary>
        /// 方法名称: ProductsTypeBLL
        /// 内容摘要: 构造函数进行初始化
        /// </summary>
        protected ProductsTypeBLL()
        {
        }
        private static volatile ProductsTypeBLL m_instance = null;
        /// <summary>
        /// 方法名称: GetInstance
        /// 内容摘要: 取得 ProductsTypeBLL 对象
        /// </summary>
        /// <returns>返回一个已经存在的实体</returns>
        public static ProductsTypeBLL GetInstance()
        {
            // 通用的必要代码 iBatisNet双校检机制,如果实例不存在
            if (m_instance == null)
            {
                lock (typeof(ProductsTypeBLL))
                {

                    // 如果实例不存在
                    if (m_instance == null)
                    {

                        // 创建一个的实例
                        m_instance = new ProductsTypeBLL();
                    }
                }
            }

            // 返回业务逻辑对象
            return m_instance;
        }

        #endregion

        #region 自动生成代码：基本业务公开函数

        /// <summary>
        /// 方法名称: Get
        /// 内容摘要: 由键值获取一个实体对象
        /// </summary>
        /// <returns>ProductsType</returns>
        public ProductsType Get(ProductsType.Key key)
        {

            return ProductsTypeDao.GetInstance().SelectByID(key);
        }
        /// <summary>
        /// 方法名称: Select
        /// 内容摘要: 基本查询，不带任何条件的查询
        /// </summary>
        public IList Select()
        {
            return ProductsTypeDao.GetInstance().SelectAll();
        }
        /// <summary>
        /// 方法名称: Insert
        /// 内容摘要: 插入一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Insert(ProductsType obj)
        {
            int result = 0;
            try
            {
                result = ProductsTypeDao.GetInstance().Insert(obj);
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

        /// <summary>
        /// 方法名称: Update
        /// 内容摘要: 更新一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Update(ProductsType obj)
        {
            ProductsTypeDao.GetInstance().Update(obj);
            return 1;
        }

        /// <summary>
        /// 方法名称: Delete
        /// 内容摘要: 删除一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Delete(ProductsType obj)
        {
            bool result = ProductsTypeDao.GetInstance().DeleteByID(new ProductsType.Key(obj.Seqno));
            return result ? 1 : 0;
        }

        #endregion

    }
    #endregion
}